home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Learn C++ (CodeWarrior) / Chap 07.06 - smartPtr / smartPtr.cp < prev    next >
Text File  |  1995-10-20  |  1KB  |  67 lines

  1. #include <iostream.h>
  2. #include <string.h>
  3.  
  4. const short kMaxNameLength = 40;
  5.  
  6.  
  7. //---------------------------------------  Name
  8.  
  9. class Name
  10. {
  11.     private:
  12.         char        first[ kMaxNameLength ];
  13.         char        last[ kMaxNameLength ];
  14.         
  15.     public:
  16.                     Name( char *lastName, char *firstName );
  17.         void    DisplayName();
  18. };
  19.  
  20. Name::Name( char *lastName, char *firstName )
  21. {
  22.     strcpy( last, lastName );
  23.     strcpy( first, firstName );
  24. }
  25.  
  26. void    Name::DisplayName()
  27. {
  28.     cout << "Name: " << first << " " << last;
  29. }
  30.  
  31.  
  32. //---------------------------------------  Politician
  33.  
  34. class Politician
  35. {
  36.     private:
  37.         Name        *namePtr;
  38.         short        age;
  39.         
  40.     public:
  41.                     Politician( Name *namePtr, short age );
  42.         Name    *operator->();
  43. };
  44.  
  45. Politician::Politician( Name *namePtr, short age )
  46. {
  47.     this->namePtr = namePtr;
  48.     this->age = age;
  49. }
  50.  
  51. Name    *Politician::operator->()
  52. {
  53.     return( namePtr );
  54. }
  55.  
  56.  
  57. //---------------------------------------  main()
  58.  
  59. int    main()
  60. {
  61.     Name                myName( "Clinton", "Bill" );
  62.     Politician    billClinton( &myName, 46 );
  63.     
  64.     billClinton->DisplayName();
  65.     
  66.     return 0;
  67. }